package nbcp.db.redis.proxy

import io.lettuce.core.RedisClient
import io.lettuce.core.ScanArgs
import io.lettuce.core.api.sync.RedisCommands
import io.lettuce.core.codec.Utf8StringCodec
import nbcp.base.utils.SpringUtil
import nbcp.db.redis.RedisConfig

/**
 * Created by yuxh on 2018/6/7
 */

open class RedisSetProxy(
        collectionName: String,
        dbOffset: Int = 0) : BaseRedisOneKeyProxy(dbOffset, collectionName) {


    fun sadd(vararg value: String) {
        if (value.any() == false) return
        redis.stringCommand(dbOffset) { it.sadd(collectionName, *value) }
    }

    fun scard(): Int {
        return redis.stringCommand(dbOffset) { it.scard(collectionName).toInt() }
    }

    fun sismember(member: String): Boolean {
        return redis.stringCommand(dbOffset) { it.sismember(collectionName, member) }
    }

    fun smembers(): Set<String> {
        return redis.stringCommand(dbOffset) { it.smembers(collectionName) }
    }

    fun spop(): String {
        return redis.stringCommand(dbOffset) { it.spop(collectionName) ?: "" }
    }

    fun sscan(member: String, limit: Int): List<String> {
        return redis.stringCommand(dbOffset) {
            it.sscan(collectionName, ScanArgs.Builder.matches(member).limit(limit.toLong())).values
        }
    }

    fun srem(vararg members: String): Long {
        return redis.stringCommand(dbOffset) { it.srem(collectionName, *members) }
    }
}